package com.gxa.servlet.activity;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.pojo.Result;
import com.gxa.pojo.activity.ActivityType;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(urlPatterns = "/TestJson")
public class TestJsonServlet extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        BufferedReader reader = request.getReader();
        StringBuilder stringBuilder = new StringBuilder();
        String a = null;
        while ((a = reader.readLine())!=null){
             stringBuilder.append(a);
         }
        String s = stringBuilder.toString();
        System.out.println("s: "+s);
        ActivityType activityType = objectMapper.readValue(s, ActivityType.class);
        System.out.println("activityType:"+activityType);

        Map<String,Object> map = new HashMap<>();
        String result = null;
        map.put("result", Result.ok("success"));
        result= objectMapper.writeValueAsString(map);

        PrintWriter writer = response.getWriter();
        writer.write(result);
        writer.flush();
        writer.close();
    }
}
